package com.shop.loan.handler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.shop.loan.mapper.LoanOrderAttachmentByMybatisMapper;
import com.shop.loan.model.entity.LoanOrderAttachmentByMybatis;
import com.shop.loan.model.vo.AttachmentUploadVo;
import org.springframework.stereotype.Service;
import xyz.erupt.annotation.fun.OperationHandler;
import xyz.erupt.upms.service.EruptUserService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class AttachmentUploadHandler implements OperationHandler<Object, AttachmentUploadVo> {

    @Resource
    LoanOrderAttachmentByMybatisMapper loanOrderAttachmentByMybatisMapper;

    @Resource
    EruptUserService eruptUserService;

    @Override
    public String exec(List<Object> data, AttachmentUploadVo attachmentUploadVo, String[] param) {
        Object item = data.get(0);
        JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(item));
        Long id = jsonObject.getLongValue("id");

        LoanOrderAttachmentByMybatis loanOrderAttachmentByMybatis = new LoanOrderAttachmentByMybatis();
        loanOrderAttachmentByMybatis.setOrderId(id);
        loanOrderAttachmentByMybatis.setCreateBy(eruptUserService.getCurrentEruptUser().getName());
        loanOrderAttachmentByMybatis.setCreateTime(new Date());
        loanOrderAttachmentByMybatis.setType(attachmentUploadVo.getType());
        loanOrderAttachmentByMybatis.setInfo(attachmentUploadVo.getInfo());
        loanOrderAttachmentByMybatis.setRemark(attachmentUploadVo.getRemark());
        loanOrderAttachmentByMybatisMapper.insertSelective(loanOrderAttachmentByMybatis);
        return null;
    }
}
